﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Logger;
using System.Diagnostics.Contracts;


namespace WindowsVHDClientNode
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }

    internal class ManualVHDTesting
    {
        private Log log;
        private System.IO.TextWriter output;
        private System.IO.TextReader input;
        private readonly String sampleText;

        public ManualVHDTesting()
        {
            log = Log.Create("Program");
            output = System.Console.Out;
            input = System.Console.In;

            Contract.Assume(output != null);
            Contract.Assume(input != null);
            sampleText = "Hello World. Nervously I loaded the twin ducks aboard the revolving platform.";

        }

        [ContractInvariantMethod]
        private void ObjectInvariant()
        {
            Contract.Invariant(log != null);
            Contract.Invariant(output != null);
            Contract.Invariant(input != null);
            Contract.Invariant(sampleText != null);
            Contract.Invariant(sampleText.Length > 4);
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        public void Main()
        {
            System.Threading.Thread.Sleep(1000);
            ConsoleLogger.Init();
            ConsoleLogger.BlackListFilter("Berlekamp");

            System.Threading.Thread VHDD = new System.Threading.Thread(new System.Threading.ThreadStart(startVHDD));
            VHDD.Start();

            System.Threading.Thread.Sleep(TimeSpan.FromSeconds(5));

            System.Threading.Thread A = new System.Threading.Thread(new System.Threading.ThreadStart(readFile));
            System.Threading.Thread B = new System.Threading.Thread(new System.Threading.ThreadStart(NoOp));

            A.Start();
            B.Start();
            output.WriteLine("Type anything to exit");
            input.ReadLine();
            stopping = true;
        }

        private void startVHDD()
        {
            System.IO.DirectoryInfo rmp = new System.IO.DirectoryInfo(Application.StartupPath + "\\SampleDrive");
            Contract.Assume(rmp.FullName.Length > 1);
            using (SampleDrive sd = new FailingDrive('s', "Sample Drive", rmp))
            {
                while (!stopping)
                {
                    System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));
                }
            }
        }

        private bool stopping = false;
        public void readFile()
        {
            while (!stopping)
            {
                try
                {
                    System.IO.FileInfo fi = new System.IO.FileInfo("s:\\test.txt");
                    using (System.IO.FileStream stream = fi.OpenRead())
                    {
                        byte[] temp = new byte[2];
                        while (stream.Read(temp, 0, 1) != 0)
                        {
                            output.Write((Char)temp[0]);
                            System.Threading.Thread.Sleep(10);
                        }
                    }
                }
                catch { }
                System.Threading.Thread.Sleep(100);
            }
        }

        public void NoOp()
        {
            while (!stopping)
            {
                output.WriteLine("NoOp");
                System.Threading.Thread.Sleep(500);
            }
        }
    }
}
